package cn.ztydata.teachingsystem.heaven.dao.redis;

import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Repository;

import java.util.Set;

/**
 * 用户收藏笔记dao
 * Created by Administrator on 2015/3/19.
 */
@Repository
public class UserNoteDao extends BaseRedisDao {
    private final static String USER_NOTE = "user:note";

    /**
     * 获取收藏笔记集合
     * @param userId ID
     * @param role 角色
     * @return set
     *
     * @author wyh
     * @since 2015-3-19
     */
    public Set<Long> getFavorite(long userId, int role){
        SetOperations<String, Long> setOperations = redisTemplate.opsForSet();

        logger.debug("返回 收藏笔记ID的集合");
        return setOperations.members(KeyUtils.generateKey(USER_NOTE, role, userId));
    }

    /**
     * 新增收藏
     * @param userId 用户ID
     * @param role 角色
     * @param noteId 笔记ID
     *
     * @author wyh
     * @since 2015-3-20
     */
    public long saveFavorite(long userId, int role, long noteId){
        SetOperations<String, Long> setOperations = redisTemplate.opsForSet();

        return setOperations.add(KeyUtils.generateKey(USER_NOTE, role, userId), noteId);
    }

    /**
     * 删除收藏
     * @param userId 用户ID
     * @param role 角色
     * @param noteId 笔记ID
     *
     * @author wyh
     * @since 2015-3-20
     */
    public long deleteFavorite(long userId, int role, long noteId) {
        SetOperations<String, Long> setOperations = redisTemplate.opsForSet();

        return setOperations.remove(KeyUtils.generateKey(USER_NOTE, role, userId), noteId);
    }
}
